package com.example.dict;

import android.graphics.Bitmap;
import android.util.Log;

public class Recognition {
	private Bitmap src;
	private int w,h,aw,ah;
	private boolean[][] bitmap = new boolean[100][5000];
	private int[][] check = new int[100][5000];
	private boolean[][] alpha = new boolean[300][300], rcg = new boolean[40][40];
	private boolean[] v = new boolean[100000];
	private String word = "";
	Recognition(Bitmap src){
		this.src=src;
		w=src.getWidth();
		h=src.getHeight();
	}
	public void findAlphabetNearPixel(int x, int y){
	}
	public void findWordNearCenter(){
	}
	public void cropWord(){
		int i,j,k,l,m;
		int head,tail,y,x,ny,nx,cnt=0;
		int[][] que = new int[5000][2];
		int[] dy = {0, -1, -1, 0, 1, 1, 1, 0, -1}, dx = {0, 0, 1, 1, 1, 0, -1, -1, -1};
		int my,My,mx,Mx;
		int ptail,pmy,pMy,pmx,pMx;
		for(j=0;j<w;j++){
			for(i=0;i<h;i++){
				if(bitmap[i][j] && !v[check[i][j]]){
					cnt++; v[cnt]=true;
					head=0; tail=1; que[1][0]=i; que[1][1]=j; check[i][j]=cnt;
					my=My=i; mx=Mx=j;
					while(head!=tail){
						head++; y=que[head][0]; x=que[head][1];
						for(k=1;k<=8;k++){
							ny=y+dy[k]; nx=x+dx[k];
							if(ny<0 || nx<0 || ny>=h || nx>=w) continue;
							if(check[ny][nx]==cnt || !bitmap[ny][nx]) continue;
							tail++; que[tail][0]=ny; que[tail][1]=nx; check[ny][nx]=cnt;
							if(ny<my) my=ny;
							if(ny>My) My=ny;
							if(nx<mx) mx=nx;
							if(nx>Mx) Mx=nx;
						}
					}
					ptail=tail; pmx=mx; pmy=my; pMx=Mx; pMy=My;
					for(k=mx;k<=Mx;k++){
						for(l=0;l<h;l++){
							if(bitmap[l][k] && check[l][k]==0){
								cnt++;
								head=0; tail=1; que[1][0]=l; que[1][1]=k; check[i][j]=cnt;
								my=My=l; mx=Mx=k;
								while(head!=tail){
									head++; y=que[head][0]; x=que[head][1];
									for(m=1;m<=8;m++){
										ny=y+dy[m]; nx=x+dx[m];
										if(ny<0 || nx<0 || ny>=h || nx>=w) continue;
										if(check[ny][nx]==cnt || !bitmap[ny][nx]) continue;
										tail++; que[tail][0]=ny; que[tail][1]=nx; check[ny][nx]=cnt;
										if(ny<my) my=ny;
										if(ny>My) My=ny;
										if(nx<mx) mx=nx;
										if(nx>Mx) Mx=nx;
									}
								}
								if(tail<h/5 || ptail<h/5){
									if(pmy<my) my=pmy;
									if(pmx<mx) mx=pmx;
									if(pMy>My) My=pMy;
									if(pMx>Mx) Mx=pMx;
									v[cnt]=true;
								}
								else{
									my=pmy; mx=pmx; My=pMy; Mx=pMx;
								}
								break;
							}
						}
					}
					aw=Mx-mx+1; ah=My-my+1;
					Log.e("here", "/"+cnt+" "+ah+" "+aw);
					for(k=my;k<=My;k++){
						for(l=mx;l<=Mx;l++){
							if(check[k][l]==cnt-1) alpha[k-my][l-mx]=true;
							else if(check[k][l]==cnt && v[cnt]) alpha[k-my][l-mx]=true;
							else alpha[k-my][l-mx]=false;
						}
					}
					resizeAlphabet();
					word += recognizeAlphabet();
				}
			}
		}
	}
	public void resizeAlphabet(){
		int i,j;
		int cnt=0,l=0,r=0;
		Bitmap al = Bitmap.createBitmap(aw, ah, Bitmap.Config.ARGB_8888);
		for(i=0;i<ah;i++){
			for(j=0;j<aw;j++){
				if(alpha[i][j]) al.setPixel(j, i, 0xffffffff);
			}
		}
		if(ah<aw){
			if(aw*ah/40==0) al = Bitmap.createScaledBitmap(al, 40, 1, false);
			else al = Bitmap.createScaledBitmap(al, 40, aw*ah/40, false);
		}
		else{
			if(ah*aw/40==0) al = Bitmap.createScaledBitmap(al, 1, 40, false); 
			else al = Bitmap.createScaledBitmap(al, ah*aw/40, 40, false);
		}
		ah=al.getHeight(); aw=al.getWidth();
		for(i=0;i<40;i++) for(j=0;j<40;j++) rcg[i][j]=false;
		for(i=0;i<ah;i++){
			for(j=0;j<aw;j++){
				if(al.getPixel(j, i)==0xffffffff) rcg[i][j]=true;
			}
		}
	}
	public char recognizeAlphabet(){
		return 'a';
	}
	public String recognizeWord(){
		int i,j;
		int px,r,g,b;
		for(i=0;i<h;i++){
			for(j=0;j<w;j++){
				px = src.getPixel(j, i);
				r = (px >> 16) & 0xff;
				g = (px >> 8) & 0xff;
				b = px & 0xff;
				if(r+g+b<500) bitmap[i][j]=true;
				else bitmap[i][j]=false;
			}
		}
		cropWord();
		return word;
	}
}
